<script setup lang="ts">
import CommonSwiper from '@/components/CommonSwiper.vue'
import CategorySkeleton from '@/pages/category/components/CategorySkeleton.vue'
import { getHomeBanner } from '@/api/home'
import { getCategoryMenu } from '@/api/category'

import { ref, computed } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import type { BannerItem } from '@/types/home'
import type { CategoryMenuItem, CategoryChildItem } from '@/types/category'

// 轮播图数据
const bannerList = ref<BannerItem[]>([])

// 获取轮播图
const getCategoryList = () => {
  getHomeBanner(2).then((res) => {
    bannerList.value = res.result
  })
}

const activeIndex = ref(0)

// 一级分类列表
const categoryList = ref<CategoryMenuItem[]>([])
const categoryChildList = computed(() => {
  return categoryList.value[activeIndex.value]?.children || []
})
// 获取一级分类数据
const getCategoryMenuList = () => {
  getCategoryMenu().then((res) => {
    categoryList.value = res.result
  })
}

// 是否加载骨架屏
const isLoading = ref(false)
onLoad(async () => {
  await Promise.all([getCategoryList(), getCategoryMenuList()])
  isLoading.value = true
})

//
</script>

<template>
  <view class="viewport" v-if="isLoading">
    <!-- 搜索框 -->
    <view class="search">
      <view class="input">
        <text class="icon-search">女靴</text>
      </view>
    </view>
    <!-- 分类 -->

    <view class="categories">
      <!-- 左侧：一级分类 -->
      <scroll-view class="primary" scroll-y>
        <view
          v-for="(item, index) in categoryList"
          :key="item.id"
          class="item"
          :class="{ active: index === activeIndex }"
          @tap="activeIndex = index"
        >
          <text class="name"> {{ item.name }} </text>
        </view>
      </scroll-view>
      <!-- 右侧：二级分类 -->
      <scroll-view class="secondary" scroll-y>
        <!-- 焦点图 -->
        <CommonSwiper class="banner" :list="bannerList" />
        <!-- 内容区域 -->
        <view class="panel" v-for="item in categoryChildList" :key="item.id">
          <view class="title">
            <text class="name">{{ item.name }}</text>
            <navigator class="more" hover-class="none">全部</navigator>
          </view>
          <view class="section">
            <navigator
              v-for="goods in item.goods"
              :key="goods.id"
              class="goods"
              hover-class="none"
              :url="`/pages/goods/goods?id=${goods.id}`"
            >
              <image class="image" :src="goods.picture"></image>
              <view class="name ellipsis"> {{ goods.name }} </view>
              <view class="price">
                <text class="symbol">¥</text>
                <text class="number">{{ goods.price }}</text>
              </view>
            </navigator>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>

  <CategorySkeleton v-else />
</template>

<style lang="scss">
@import './styles/category.scss';
</style>
